/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.nifi.processors.huawei.obs.model;

import com.obs.services.ObsClient;
import com.obs.services.model.*;
import org.apache.nifi.processor.ProcessContext;

import java.util.ArrayList;
import java.util.List;

import static org.apache.nifi.processors.huawei.common.PropertyDescriptors.*;

public class OBSObjectBucketLister implements ObsBucketLister{
    private final ObsClient client;
    private final ListObjectsRequest listObjectsRequest;
    private ObjectListing objectListing;

    public OBSObjectBucketLister(ObsClient client, ProcessContext context) {
        this.client = client;
        final String bucket = context.getProperty(BUCKET).evaluateAttributeExpressions().getValue();
        final String delimiter = context.getProperty(DELIMITER).getValue();
        final String prefix = context.getProperty(PREFIX).evaluateAttributeExpressions().getValue();
        listObjectsRequest = new ListObjectsRequest(bucket, prefix, null, delimiter, 0, null);
    }

    @Override
    public ListVersionsResult listVersions() {
        this.objectListing = client.listObjects(listObjectsRequest);
        List<VersionOrDeleteMarker> summaryList = new ArrayList<>();
        for (ObsObject objectSummary : objectListing.getObjects()) {
            VersionOrDeleteMarker versionSummary = new VersionOrDeleteMarker.Builder()
                    .bucketName(objectSummary.getBucketName())
                    .lastModified(objectSummary.getMetadata().getLastModified())
                    .owner(objectSummary.getOwner())
                    .size(objectSummary.getMetadata().getContentLength())
                    .storageClass(objectSummary.getMetadata().getObjectStorageClass())
                    .isLatest(true)
                    .key(objectSummary.getObjectKey()).builder();
            summaryList.add(versionSummary);
        }
        return new ListVersionsResult.Builder().versions(summaryList.toArray(new VersionOrDeleteMarker[0])).builder();
    }

    @Override
    public void setNextMarker() {
        listObjectsRequest.setMarker(objectListing.getNextMarker());
    }

    @Override
    public boolean isTruncated() {
        return objectListing != null && objectListing.isTruncated();
    }
}
